import { ref, onMounted, watch } from 'vue'
import { useDictStore } from '@/store/modules/dict'

/**
 * 字典Hooks
 * @param {string|Array<string>} dictTypes 字典类型，可以是单个字典类型或字典类型数组
 * @returns {{dicts: Object, loadDict: Function}} 字典数据对象和加载方法
 * @example
 * // 单个字典
 * const { dicts } = useDict('sys_user_sex')
 * // 访问方式: dicts.sys_user_sex
 * 
 * // 多个字典
 * const { dicts } = useDict(['sys_user_sex', 'sys_normal_disable'])
 * // 访问方式: dicts.sys_user_sex, dicts.sys_normal_disable
 */
export default function useDict(...dictTypes) {
  const dictStore = useDictStore()
  const dicts = ref({})
  const loading = ref(false)
  
  const loadDict = async () => {
    // 将传入的参数扁平化处理，支持数组或多个参数
    const types = dictTypes.flat(1)
    
    if (types.length === 0) {
      return
    }
    
    loading.value = true
    try {
      const dictData = await dictStore.getDicts(types)
      
      // 更新字典数据
      types.forEach(type => {
        dicts.value[type] = dictData[type] || []
      })
    } finally {
      loading.value = false
    }
  }
  
  onMounted(() => {
    loadDict()
  })
  
  return {
    dicts,
    loading,
    loadDict
  }
} 